package com.loveyukari.controller;

import com.loveyukari.service.AliCloudUploadService;
import com.loveyukari.support.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RequestMapping("/admin/upload")
@RestController
public class UploadController {


    @Autowired
    private AliCloudUploadService aliCloudUploadService;

    @RequestMapping("/image")
    public JsonResult image(String type,MultipartFile image){
        String fileName = image.getOriginalFilename();
        fileName = "image/"+type+"/"+System.currentTimeMillis()+fileName.substring(fileName.lastIndexOf("."));
        try {
            aliCloudUploadService.upload(image.getInputStream(),fileName);
            return new JsonResult(0,"",fileName);
        } catch (IOException e) {
            return new JsonResult(-1,"上传失败");
        }
    }


}
